/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */
package com.hopes.bizz.basic.hazard.process.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hopes.bizz.basic.common.constant.enums.ParkBasicSysDictEnum;
import com.hopes.bizz.basic.hazard.process.dto.HazProcessExcelDTO;
import com.hopes.bizz.basic.hazard.process.entity.HazProcessEntity;
import com.hopes.bizz.basic.hazard.process.mapper.HazProcessMapper;
import com.hopes.bizz.basic.hazard.process.service.HazProcessService;
import com.hopes.bizz.basic.hazard.process.vo.HazProcessExcelVO;
import com.hopes.bizz.basic.hazard.process.vo.HazProcessTypeGisVo;
import com.hopes.boss.admin.api.resolver.DictResolver;
import com.hopes.boss.common.entity.ValidateImportBO;
import com.hopes.boss.common.excel.vo.ErrorMessage;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 危险工艺
 *
 * @author daijie
 * @date 2023-11-10 16:01:14
 */
@Service
public class HazProcessServiceImpl extends ServiceImpl<HazProcessMapper, HazProcessEntity> implements HazProcessService {

    @Override
    public ValidateImportBO<HazProcessExcelDTO> validateImport(List<HazProcessExcelDTO> excelDTOList) {
        ValidateImportBO<HazProcessExcelDTO> bo = new ValidateImportBO<>();
        List<ErrorMessage> errorMessageList = new ArrayList<>();
        List<HazProcessExcelDTO> saveList = new ArrayList<>();
        int successNum = 0;
        int failedNum = 0;

        bo.setErrorMessages(errorMessageList);
        bo.setSaveList(saveList);
        bo.setSuccessNum(successNum);
        bo.setFailedNum(failedNum);
        return bo;
    }

    @Override
    public HazProcessEntity importDtoToEntity(HazProcessExcelDTO excelDto) {
        HazProcessEntity entity = new HazProcessEntity();
        BeanUtils.copyProperties(excelDto, entity);
        return entity;
    }

    @Override
    public HazProcessExcelVO exportEntityToVo(HazProcessEntity entity) {
        HazProcessExcelVO excelVO = new HazProcessExcelVO();
        BeanUtils.copyProperties(entity, excelVO);
        return excelVO;
    }

    @Override
    public HazProcessTypeGisVo getHazProcessType() {
        HazProcessTypeGisVo hazProcessTypeGisVo = new HazProcessTypeGisVo();
        JSONArray pieData = new JSONArray();
        List<HazProcessEntity> hazProcessEntities = baseMapper.selectList(null);
        // 根据工艺类型统计
        Map<String, Long> hazProcessMap = hazProcessEntities.stream().collect(Collectors.groupingBy(HazProcessEntity::getProcessType, Collectors.counting()));
        hazProcessMap.forEach((k, v) -> {
            JSONObject data = new JSONObject();
            data.put("name", DictResolver.getItemLabelByValue(ParkBasicSysDictEnum.hazard_chemical_process,k));
            data.put("value",v);
            pieData.add(data);
        });
        hazProcessTypeGisVo.setHazProcessCount(hazProcessEntities.size());
        hazProcessTypeGisVo.setPieData(pieData);
        return hazProcessTypeGisVo;
    }
}
